package cn.iocoder.yudao.module.nginxui.service.httplocation;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.nginxui.dal.dataobject.httpconfig.HttpConfigDO;
import cn.iocoder.yudao.module.nginxui.dal.dataobject.httpserver.HttpServerDO;
import cn.iocoder.yudao.module.nginxui.dal.mysql.httpconfig.HttpConfigMapper;
import cn.iocoder.yudao.module.nginxui.dal.mysql.httpserver.HttpServerMapper;
import cn.iocoder.yudao.module.nginxui.service.httpconfig.HttpConfigSyncService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.nginxui.controller.admin.httplocation.vo.*;
import cn.iocoder.yudao.module.nginxui.dal.dataobject.httplocation.HttpLocationDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.nginxui.dal.mysql.httplocation.HttpLocationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.nginxui.enums.ErrorCodeConstants.*;

/**
 * Nginx Server下的Location块配置 Service 实现类
 *
 * @author HuangJunZhong
 */
@Service
@Validated
public class HttpLocationServiceImpl implements HttpLocationService {

    @Resource
    private HttpLocationMapper httpLocationMapper;
    @Resource
    private HttpServerMapper httpServerMapper;
    @Resource
    private HttpConfigMapper httpConfigMapper;
    @Resource
    private HttpConfigSyncService httpConfigSyncService;

    @Override
    @Transactional
    public Long createHttpLocation(HttpLocationSaveReqVO createReqVO) {
        HttpLocationDO location = BeanUtils.toBean(createReqVO, HttpLocationDO.class);
        httpLocationMapper.insert(location);

        // 2. 逐级获取主配置ID（Location→Server→HTTP→主配置）
        syncToMainConfig(location.getServerId());
        return location.getId();
    }

    @Override
    @Transactional
    public void updateHttpLocation(HttpLocationSaveReqVO updateReqVO) {
        // 1. 业务逻辑（校验、更新等）
        HttpLocationDO location = validateHttpLocationExists(updateReqVO.getId());
        BeanUtils.copyProperties(updateReqVO, location);
        httpLocationMapper.updateById(location);

        // 2. 同步到主配置
        syncToMainConfig(location.getServerId());
    }

    @Override
    @Transactional
    public void deleteHttpLocation(Long id) {
        // 1. 业务逻辑（校验、删除等）
        HttpLocationDO location = validateHttpLocationExists(id);
        httpLocationMapper.deleteById(id);

        // 2. 同步到主配置
        syncToMainConfig(location.getServerId());
    }

    @Override
        public void deleteHttpLocationListByIds(List<Long> ids) {
        // 删除
        httpLocationMapper.deleteByIds(ids);
        }


    private HttpLocationDO validateHttpLocationExists(Long id) {
        HttpLocationDO location = httpLocationMapper.selectById(id);
        if (location== null) {
            throw exception(HTTP_LOCATION_NOT_EXISTS);
        }
        return location;
    }

    @Override
    public HttpLocationDO getHttpLocation(Long id) {
        return httpLocationMapper.selectById(id);
    }

    @Override
    public PageResult<HttpLocationDO> getHttpLocationPage(HttpLocationPageReqVO pageReqVO) {
        return httpLocationMapper.selectPage(pageReqVO);
    }
    /**
     * 逐级关联获取主配置ID并同步
     */
    private void syncToMainConfig(Long serverId) {
        HttpServerDO server = httpServerMapper.selectById(serverId);
        if (server == null) {
            return;
        }
        HttpConfigDO httpConfig = httpConfigMapper.selectById(server.getHttpId());
        if (httpConfig != null) {
            httpConfigSyncService.syncToMainConfig(httpConfig.getConfigId());
        }
    }
}